<?php
/*
require_once 'Sabai/Locator/Interface.php';
*/
class Sabai_Application /*implements Sabai_Locator_Interface*/
{
    /**
     * @access protected
     */
    var $_name;
    var $_path;
    var $_config = array();
    var $_roles = array();
    var $_services = array();

    function Sabai_Application($name, $path)
    {
        $this->_name = $name;
        $this->_path = $path;
    }

    function &getInstance($appName)
    {
        static $apps = array();
        $app_name = strtolower($appName);
        if (!isset($apps[$app_name])) {
            $apps[$app_name] =& new $appName();
            bindtextdomain($app_name, $apps[$app_name]->getPath() . '/locales');
            bind_textdomain_codeset($app_name, SABAI_CHARSET);
        }
        textdomain($app_name);
        return $apps[$app_name];
    }

    function runWeb(&$app, &$controller, &$user, $layout)
    {
        require_once 'Sabai/Request/Web.php';
        require_once 'Sabai/Response/Web.php';
        require_once 'Sabai/Template/Default.php';
        $app_path = $app->getPath();
        $layout_dir = 'layouts/' . $layout;
        $template =& new Sabai_Template_Default(
                        $app_path . '/templates',
                        $app_path . '/templates/helpers');
        if (is_dir($template_dir = $layout_dir . '/templates')) {
            $template->addTemplateDir($template_dir);
        }
        $request =& new Sabai_Request_Web();
        $response =& new Sabai_Response_Web($template, SABAI_URL_DIR . '/' . $layout_dir, $layout_dir);
        $response->setPartial($request->isPartial());
        Sabai_Application::run($app, $controller, $request, $response, $user);
    }

    function run(&$app, &$controller, &$request, &$response, &$user)
    {
        require 'Sabai/Controller/Context.php';
        $context =& new Sabai_Controller_Context($request, $response, $user, $app->getConfig());
        $response->setVars(array('REQUEST' => &$context->request,
                                 'USER' => &$context->user,
                                 'CONFIG' => &$context->config));
        $controller->setLocator($app);
        $controller->execute($context);
        $response->send();
    }

    function getName()
    {
        return $this->_name;
    }

    function getPath()
    {
        return $this->_path;
    }

    function &getService($serviceName)
    {
        if (!isset($this->_services[$serviceName])) {
            $method = '_get' . $serviceName;
            $this->_services[$serviceName] =& $this->$method();
        }
        return $this->_services[$serviceName];
    }

    function setConfig($name, $value)
    {
        $this->_config[$name] = $value;
    }

    function &getConfig()
    {
        require_once 'Sabai/Config/Array.php';
    	$config =& new Sabai_Config_Array($this->_config);
    	return $config;
    }

    function _addRole($roleId, $roleName)
    {
        $this->_roles[$roleId] = $roleName;
    }

    function getRoles()
    {
        return $this->_roles;
    }

    function debug($level = SABAI_LOG_ALL)
    {
        Sabai_Log::level($level);
        require_once 'Sabai/Log/Writer/HTML.php';
        Sabai_Log::writer(new Sabai_Log_Writer_HTML());
    }
}